import {
	Config
} from './config.js';
import store from '../../store'

export function Http(params) {
	return new Promise((resolved, rejected) => {
		var options = {}
		options.method = params.type ? params.type : 'GET'
		options.url = Config.http_static + Config.http_api[params.url];
		if (params.urldata != undefined) {
			options.url = options.url + '/' + params.urldata
		}
		if (params.data != undefined) {
			options.data = params.data
		} else {
			options.data = {}
		}
		options.header = {
			'Accept': 'application/json',
			'content-type': options.method === 'POST' ? 'application/x-www-form-urlencoded' :
				'application/json',
		};

		if (params.token == undefined || params.token) {
			console.log('jinlai')
			if (uni.getStorageSync('token')) {
				options.data.token = uni.getStorageSync('token')
			}
			// #ifdef H5
			// options.data.token = 'ff605e97-21ba-454b-9183-14294a5025bf' //63
			// options.data.token = '0aff032b-63f7-42af-bf47-3f81d24ce008' //10
			
			// options.data.token = 'b5992cd5-dccd-4e2e-8ec0-2228ff7f1cdc' //52
			// options.data.token = 'b1aa0498-9851-4095-8365-4f166f63eab0' //57
			// #endif
		}

		if (params.load == undefined || params.load) {
			uni.showLoading({
				title: '加载中',
				mask: true
			});
		}

		options.success = (res) => {
			if (params.load == undefined || params.load) {
				uni.hideLoading();
			}
			console.log(options.url, options, res)
			//200为接口成功
			if (res.statusCode == 200) {
				//code为接口返回正确
				if (res.data.code == 0) {
					//弹出错误信息
					if (params.show) {
						uni.showToast({
							icon: 'none',
							duration: 2000,
							title: res.data.msg
						});
					}
				} else if (res.data.code == 101 || res.data.code == 102) {
					let content = ''
					if (res.data.code == 101) {
						content = '请先登录后在查看'
					}
					if (res.data.code == 102) {
						content = '您得登录信息已过期,请重新登录'
					}
					// if (params.noForceLogin == undefined) {
					if(!uni.$store.state.showLoginModal){
						uni.$store.commit('changeShowLoginModal', true)
						uni.showModal({
							title: '登录提示',
							confirmText: '去登录',
							confirmColor: '#6ccbb6',
							cancelText: '首页',
							content: content,
							success: function(res) {
								if (res.confirm) {
									let pages = getCurrentPages()
									let beforePage = pages[pages.length - 1].route;
									let Pageoptions = pages[pages.length - 1].options
									if (Pageoptions == {}) {
										beforePage = beforePage
									} else {
										let num = 0
										for (let key in Pageoptions) {
											num++
											if (num > 1) {
												beforePage = beforePage + '&' + key + '=' +
													Pageoptions[key]
											} else {
												beforePage = beforePage + '?' + key + '=' +
													Pageoptions[key]
											}
										}
									}
									store.commit('Onvuexurl', beforePage)
									uni.redirectTo({
										url: '/pagesA/common/Signin'
									});
								} else if (res.cancel) {
									uni.removeStorage({
										key: 'token',
										success: function(res) {
											uni.switchTab({
												url: '/pages/home/index'
											});
										}
									});
								}
								uni.removeStorageSync('userInfo');
							},
							complete() {
								uni.$store.commit('changeShowLoginModal', false)
							}
						});
					}
					// }
				}
				resolved(res.data);
			} else {
				uni.showToast({
					icon: 'none',
					duration: 3000,
					title: '接口异常'
				});
			}
		}
		//错误
		options.fail = (err) => {
			rejected(err); //错误
		}
		uni.request(options);
	});
}
